package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.dao.HospitalDao;
import com.bed.lovewithpatientbed.pojo.Address;
import com.bed.lovewithpatientbed.pojo.BedPutExample;
import com.bed.lovewithpatientbed.pojo.Hospital;
import com.bed.lovewithpatientbed.pojo.HospitalExample;
import com.bed.lovewithpatientbed.service.AddressService;
import com.bed.lovewithpatientbed.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
 * @author yg
 * @version 1.0.0
 * @ClassName HospitalServiceImpl.java
 * @Description TODO
 */

@Service
@Transactional
public class HospitalServiceImpl implements HospitalService {

    @Resource
    private HospitalDao hospitalDao;
    // @Resource
    // private AddressServiceImpl AddressServiceImpl;
    @Autowired
    private AddressService addressService;

    @Override
    public List<Hospital> getAll() {
        List<Hospital> hospitalList = hospitalDao.selectByExample(null);
        return hospitalList;
    }

    @Override
    public List<Hospital> getHospitalBySelect(LayUiPageBean pageBean, Hospital hospital, Double count1, Double count2,Address address) {
        HospitalExample hospitalExample=new HospitalExample();
        HospitalExample.Criteria criteria = hospitalExample.createCriteria();

        // 添加分页
        if (pageBean != null && pageBean.getPage() > 0 && pageBean.getLimit() > 0) {
            hospitalExample.setOffset((pageBean.getPage() - 1) * pageBean.getLimit());
            hospitalExample.setLimit(pageBean.getLimit().intValue());
        }

        //根据医院名字模糊查询
        if(hospital.getHospitalName()!=null&&!"".equals(hospital.getHospitalName())){
            criteria.andHospitalNameLike("%"+hospital.getHospitalName()+"%");
        }
        //根据医院地址查询
        if(hospital.getAddress()!=null&&!"".equals(hospital.getAddress())){
            criteria.andAddressIdEqualTo(hospital.getAddress().getAddressId());
        }
        //根据收益分成查询
        if(count1!=null&&!"".equals(count1)){
            criteria.andHospitalGainsharingGreaterThanOrEqualTo(count1);
        }
        if(count2!=null&&!"".equals(count2)){
            criteria.andHospitalGainsharingLessThanOrEqualTo(count2);
        }
        //按省份查询
        if (address.getProvince()!=null&&!"".equals(address.getProvince())){
            List<Address> selectByProvince = addressService.selectByProvince(address.getProvince());
            List<Integer> provinceList=new ArrayList<>();
            for (Address provincemsg:selectByProvince
                 ) {
                Integer addressId = provincemsg.getAddressId();
                provinceList.add(addressId);
            }
            criteria.andAddressIdIn(provinceList);
        }

        //按地市查询
        if (address.getCity()!=null&&!"".equals(address.getCity())){
            List<Address> selectByProvince = addressService.getAddressBySelect(address);
            List<Integer> cityList=new ArrayList<>();
            for (Address provincemsg:selectByProvince
            ) {
                Integer addressId = provincemsg.getAddressId();
                cityList.add(addressId);
            }
            criteria.andAddressIdIn(cityList);
        }

        //按县区查询
        if (address.getDistrict()!=null&&!"".equals(address.getDistrict())){
            List<Address> selectByProvince = addressService.getAddressBySelect(address);
            List<Integer> districtList=new ArrayList<>();
            for (Address provincemsg:selectByProvince
            ) {
                Integer addressId = provincemsg.getAddressId();
                districtList.add(addressId);
            }
            criteria.andAddressIdIn(districtList);
        }

        //softdel为1才展示
        criteria.andSoftdelEqualTo(1);

        return hospitalDao.selectByExample(hospitalExample);
    }

    //根据医院id查找
    @Override
    public Hospital selectById(Integer hid) {
        return hospitalDao.selectByPrimaryKey(hid);
    }

    //新增医院信息
    @Override
    public int addHospital(Hospital hospital) {
        int countRow = hospitalDao.insertSelective(hospital);
        return countRow;
    }



    //根据医院id删除医院信息
    @Override
    public int delHospital(Hospital hospital) {
        hospital.setSoftdel(0);
        int countRow = hospitalDao.insert(hospital);
        return countRow;
    }

    //根据医院id批量删除医院信息
    @Override
    public int delbatchHospital(Integer[] ids) {
        int countRow=0;
        if(ids.length!=0){
            for (Integer hospitalid:ids) {
                Hospital hospital = hospitalDao.selectByPrimaryKey(hospitalid);
                hospital.setSoftdel(0);
                hospitalDao.updateByPrimaryKeySelective(hospital);
                countRow +=1;
            }
        }
        return countRow;
    }

    @Override
    public int updateHospital(Hospital hospital) {
        return hospitalDao.updateByPrimaryKeySelective(hospital);
    }
    /**
     * @description 按照条件查询所有医院
     * @author tyl
     * @updateTime 2020/10/14 14:29
     */
    @Override
    public List<Hospital> selectByExample(String sequence,String province) {
          List<Address> addressList = new ArrayList<>();
        /*查出地址的集合*/
         addressList = addressService.selectByProvince(province);
        /*获得所有地址的id*/
        List<Integer> addressIds = new ArrayList<Integer>();
        for (Address address1 :addressList) {
            addressIds.add(address1.getAddressId());
        }
        /*添加排序条件*/
        HospitalExample example = new HospitalExample();
        HospitalExample.Criteria criteria = example.createCriteria();
        List<Hospital> hospitalList = new ArrayList<>();
        if(sequence!=null&&!"".equals(sequence)&&sequence.equals("默认")){
            example.setOrderByClause(" hospital_rent asc");
        }
        if(sequence!=null&&!"".equals(sequence)&&sequence.equals("由高到低")){
            example.setOrderByClause(" hospital_rent desc");
        }
        /*添加地址条件*/
        if(addressList!=null){
            criteria.andAddressIdIn(addressIds);
        }
        hospitalList = hospitalDao.selectByExample(example);
        return hospitalList;
    }

    @Override
    public long countRow() {
        return hospitalDao.countByExample(null);
    }

    /**
     * @title getBedPutIds
     * @description
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public List<Integer> getBedPutIds(Integer hospitalId) {
        BedPutExample bedPutExample = new BedPutExample();
        BedPutExample.Criteria criteria = bedPutExample.createCriteria();
        criteria.andHospitalIdEqualTo(hospitalId);

        return null;
    }

    @Override
    public Hospital selectByManagerId(Integer hospitalManagerId) {
        HospitalExample hospitalExample=new HospitalExample();
        HospitalExample.Criteria criteria = hospitalExample.createCriteria();
        criteria.andHospitalManagerIdEqualTo(hospitalManagerId);
        List<Hospital> hospitalList = hospitalDao.selectByExample(hospitalExample);
        return hospitalList.get(0);
    }

    /**
     * @description 根据省份查医院的数量
     * @author tyl
     * @updateTime 2020/10/16 14:25 
     * @param
     */
    @Override
    public List<Integer> selectByProvinces(List<String> province){
         /*查出省份对应的地址信息*/
        List<Address> addressList = addressService.selectByProvinceList(province);
        List<Integer> hospitalNum = new ArrayList<>();
        for (Address address :addressList) {
            /*根据对应的id查出医院数量*/
            HospitalExample example = new HospitalExample();
            HospitalExample.Criteria criteria = example.createCriteria();
            criteria.andAddressIdEqualTo(address.getAddressId());
            hospitalNum.add((int) hospitalDao.countByExample(example));
        }
        return hospitalNum;
    }
}
